package cn.tang.tframe.mq.kafka.serial;

import cn.tang.tframe.mq.IQtMessager;
import cn.tang.tframe.serial.kryo.KryoObjectOutput;
import org.apache.kafka.common.serialization.Serializer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * Created by tanghc on 17/2/4.
 */
public class QtMessageSerializer implements Serializer<IQtMessager>{
	@Override
	public void configure(Map<String, ?> configs, boolean isKey) {

	}

	@Override
	public byte[] serialize(String topic, IQtMessager data) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
		KryoObjectOutput output = new KryoObjectOutput(outputStream);
		try {
			output.writeObject(data);
			output.flushBuffer();
		} catch (IOException e) {
			throw new RuntimeException("serilize qtmessager ioexception",e);
		}
		return outputStream.toByteArray();
	}

	@Override
	public void close() {

	}
}
